home *** CD-ROM | disk | FTP | other *** search
- Path: news1.cle.ab.com!usenet
- From: don.phillips@ab.com (Donald-Anthony C. Phillips)
- Newsgroups: comp.lang.c
- Subject: Re: read/write integers to files
- Date: Fri, 08 Mar 1996 22:33:29 GMT
- Organization: The Allen-Bradley Co., Inc
- Distribution: inet
- Message-ID: <4hq1t6$40d@news1.cle.ab.com>
- References: <313EBF65.4E82@www.inia.net.au>
- NNTP-Posting-Host: abpc386.cle.ab.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- Jason Collins <jason@www.inia.net.au> wrote:
-
- >Hi there,
-
- >I would preface this question with the standard "dumb newbie question......." or whatever but I
- >think you'll work it out for yourselves soon enough.
-
- >My problem is that I'm trying to write a program that will read an integer from the file
- >count.dat, increment the integer then write it back to count.dat. I have provided the listing so
- >that you can all tell me what I'm doing wrong.
-
- >Thanks....and.....be gentle.
-
- >--------------------
- >counter.c
- >--------------------
-
- >#include <stdio.h>
- >#include <stdlib.h>
-
- >FILE *filePtr;
-
- >void main()
- >{
- > char ctr;
-
- > filePtr=fopen("count.dat", "ab");
- > fscanf(filePtr, "%d", &ctr);
- > fclose(filePtr);
-
- > ctr++;
-
- > filePtr=fopen("count.dat", "wb");
- > fprintf(filePtr, "%d", ctr);
- > fclose(filePtr);
- >}
- ..........................................
- Jason,
- Here are a few observations of your source code. You may aslo want to
- post any error messages you have.
-
- Peolpe get real testy about Mains declaration:
-
- int main(void)
-
- Be sure to check the results of your fopen:
-
- if( (filePtr = fopen("count.dat","wb") ) == NULL) {
- printf("error opening file/n");
- exit(1);
- }
-
- You seem to be on the right track.
-
-
- Donald-Anthony C. Phillips
- Programmer/Analyst
- Rockwell Automation
- ---------------------------------------
- Allen-Bradley
- don.phillips@ab.com
-
-